package com.youkeda.test.j3.c9;

import com.alibaba.fastjson.JSON;
import com.youkeda.test.j3.c9.model.Weather;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * 天气数据爬取
 *
 * @author CoderXshuai
 */
public class WeatherPicker {
    public String getContent(String url) {
        String result = "";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Weather getWeather(String stringJson) {
        return JSON.parseObject(stringJson, Weather.class);
    }
}
